<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
    <meta name="renderer" content="webkit" />

    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="format-detection" content="telephone=no" />
    <meta http-equiv="Expires" content="-1"/>

    <meta http-equiv="Cache-Control" content="no-cache"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <title>js 正则</title>
    <link rel="shortcut icon" href="/favicon.ico"/>

    <style type="text/css" id="style">
    </style>

</head>
<body>

<header>
        jackdizhu 主页
    </header>
    <div class="content">

        <h5><span>js 正则</span></h5>
        <div class="clearfix">
            <pre>
                // 转为两位小数的浮点数字符串
                function priceSerialization(str) {
                    var str = str.replace(/(^\s)|(\s$)/g,'');
                    var _strA = str.match(/^[$|￥]?([0-9]+)([.]([0-9]{2}))?$/);
                    if(_strA){
                        _strA[2] = _strA[2] ? _strA[2] : '.00';
                        return _strA[1] + _strA[2];
                    }else{
                        return '0.00';
                    }
                }
                // 将字符串转为每三位数一个 , 的字符串
                function priceSerialization2(str) {
                    var str = str.replace(/(^\s)|(\s$)/g,'');
                    var _strA = str.match(/^[$|￥]?([0-9]+)([.]([0-9]{2}))?$/);
                    var _strA2;
                    if(_strA){
                      _strA[2] = _strA[2] ? _strA[2] : '.00';
                      _strA2 = priceSerialization3(_strA[1]) + _strA[2];
                    }else{
                      _strA2 = '0.00';
                    }
                    return _strA2;
                }
                // 将整数部分转为每三位数一个 , 的字符串
                function priceSerialization3(str) {
                    var str = str.replace(/(^\s)|(\s$)/g,'');
                    var _strA = str.match(/^[$|￥]?([0-9]+)$/);
                    var _strA2;
                    var _strA3;
                    if(_strA && _strA[1].length>3){
                      // 每三位数 增加 ,
                      _strA2 = _strA[1].match(/([0-9]+)([0-9]{3})$/);
                      if(_strA2[1]){
                        _strA3 = priceSerialization3(_strA2[1]) +','+_strA2[2];
                      }else{
                        _strA3 = _strA2[2];
                      }
                      return _strA3;
                    }else if(_strA && _strA[1].length<=3){
                      return _strA[1];
                    }else{
                      return '0';
                    }
                }

                var _t = [' $22.14 ',' ￥2214 ',' ￥214 ',' ￥22254846314 ',' ￥846314 ',' $22 ',' $2 3 '];
                for (var i = 0; i < _t.length; i++) {
                  console.log(priceSerialization(_t[i]));
                }
                // ['22.14','2214.00','214.00','22254846314.00','846314.00','22.00','0.00']
                var _t = [' $22.14 ',' ￥2214 ',' ￥214 ',' ￥22254846314 ',' ￥846314 ',' $22 ',' $2 3 '];
                for (var i = 0; i < _t.length; i++) {
                  console.log(priceSerialization2(_t[i]));
                }
                // ['22.14','2,214.00','214.00','22,254,846,314.00','846,314.00','22.00','0.00']

                // -----------------JS原生 对象扩展写法--------------------------------------------
                // 去左右空格
                String.prototype._trim = function () {
                    var _this = this.replace(/(^\s)|(\s$)/g,'');
                    return _this;
                }
                // 转为两位小数的浮点数字符串
                String.prototype._priceDecimal2 = function () {
                    var _strA = this.match(/^[$|￥]?([0-9]+)([.]([0-9]{2}))?$/);
                    var _this = '';
                    if(_strA){
                        _strA[2] = _strA[2] ? _strA[2] : '.00';
                        _this =  _strA[1] + _strA[2];
                    }else{
                        _this =  '0.00';
                    }
                    return _this;
                }
                // 将字符串转为每三位数一个 , 的字符串
                String.prototype._priceSerialization = function () {
                    // 将整数部分转为每三位数一个 , 的字符串
                    function priceSerialization(str) {
                        var str = str.replace(/(^\s)|(\s$)/g,'');
                        var _strA = str.match(/^[$|￥]?([0-9]+)$/);
                        var _strA2;
                        var _strA3;
                        if(_strA && _strA[1].length>3){
                          // 每三位数 增加 ,
                          _strA2 = _strA[1].match(/([0-9]+)([0-9]{3})$/);
                          if(_strA2[1]){
                            _strA3 = priceSerialization(_strA2[1]) +','+_strA2[2];
                          }else{
                            _strA3 = _strA2[2];
                          }
                          return _strA3;
                        }else if(_strA && _strA[1].length<=3){
                          return _strA[1];
                        }else{
                          return '0';
                        }
                    }
                    var _strA = this.match(/^[$|￥]?([0-9]+)([.]([0-9]{2}))?$/);
                    var _this = '';
                    if(_strA){
                      _strA[2] = _strA[2] ? _strA[2] : '.00';
                      _this = priceSerialization(_strA[1]) + _strA[2];
                    }else{
                      _this = '0.00';
                    }
                    return _this;
                }
                var _t = [' $22.14 ',' ￥2214 ',' ￥214 ',' ￥22254846314 ',' ￥846314 ',' $22 ',' $2 3 '];
                for (var i = 0; i < _t.length; i++) {
                  console.log(_t[i]._trim()._priceDecimal2()._priceSerialization());
                }
                // ['22.14','2,214.00','214.00','22,254,846,314.00','846,314.00','22.00','0.00']

                // 数字高位补零 [01]
                Number.prototype._lenWithZero = function(oCount) {
                    var strText = this.toString();
                    while (strText.length < oCount) {
                        strText = '0' + strText;
                    }
                    return strText;
                };
                var n = 9;
                console.log(n._lenWithZero(2));
                // 09

                // 时间对象 转 ['2017年08月05日11时59分12秒|星期六']
                Date.prototype._getCNDate = function() {
                    var _DayS = '一二三四五六日';
                    var oDateText = '';
                    oDateText += this.getFullYear()._lenWithZero(4) + '年';
                    oDateText += (this.getMonth() + 1)._lenWithZero(2) + '月';
                    oDateText += this.getDate()._lenWithZero(2) + '日';
                    oDateText += this.getHours()._lenWithZero(2) + '时';
                    oDateText += this.getMinutes()._lenWithZero(2) + '分';
                    oDateText += this.getSeconds()._lenWithZero(2) + '秒';
                    oDateText += '|' + '星期' + _DayS.substr(this.getDay() - 1, 1);
                    return oDateText;
                };
                var d = new Date();
                console.log(d._getCNDate());
                // 2017年08月05日12时09分48秒|星期六

            </pre>
        </div>

    </div>

    <script type="text/javascript" src="./dist/index.js"></script>
     <script type="text/javascript">

     </script>
</body>
</html>
